package com.ruoyi.project.system.order.controller;

import java.util.List;

import com.ruoyi.project.system.item.domain.ZzCustomerOrderItem;
import com.ruoyi.project.system.item.service.IZzCustomerOrderItemService;
import com.ruoyi.project.system.product.domain.ZzProduct;
import com.ruoyi.project.system.product.service.IZzProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.order.domain.ZzCustomerOrder;
import com.ruoyi.project.system.order.service.IZzCustomerOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-10-28
 */
@Controller
@RequestMapping("/system/order")
public class ZzCustomerOrderController extends BaseController
{
    private String prefix = "system/order";

    @Autowired
    private IZzCustomerOrderService zzCustomerOrderService;

    @Autowired
    private IZzCustomerOrderItemService zzCustomerOrderItemService;

    @Autowired
    private IZzProductService zzProductService;


    @RequiresPermissions("system:order:view3")
    @GetMapping("/lackorder")
    public String lackorder()
    {
        return prefix + "/lackorder";
    }

    @RequiresPermissions("system:order:view2")
    @GetMapping("/getorder")
    public String getorder()
    {
        return prefix + "/getorder";
    }

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    //为下拉框查询
    @RequiresPermissions("system:order:findAllZzCustomerOrder")
    @PostMapping("/findAllZzCustomerOrder")
    @ResponseBody
    public List<ZzCustomerOrder> findAllZzCustomerOrder() {
        return zzCustomerOrderService.findAllZzCustomerOrder();
    }

    //实现接单与判断是否货源充足的操作
    @RequiresPermissions("system:order:changeOneOrder")
    @PostMapping("/changeOneOrder")
    @ResponseBody
    public Integer changeOneOrder(ZzCustomerOrder zzCustomerOrder) {
        return zzCustomerOrderService.changechangeOneOrder(zzCustomerOrder);
    }

    //单独查询订单状态
    @RequiresPermissions("system:order:findOneOrder")
    @PostMapping("/findOneOrder")
    @ResponseBody
    public ZzCustomerOrder findOneOrder(Integer customerOrderId) {
        return zzCustomerOrderService.findOneOrder(customerOrderId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZzCustomerOrder zzCustomerOrder)
    {
        startPage();
        List<ZzCustomerOrder> list = zzCustomerOrderService.selectZzCustomerOrderList(zzCustomerOrder);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZzCustomerOrder zzCustomerOrder)
    {
        List<ZzCustomerOrder> list = zzCustomerOrderService.selectZzCustomerOrderList(zzCustomerOrder);
        ExcelUtil<ZzCustomerOrder> util = new ExcelUtil<ZzCustomerOrder>(ZzCustomerOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZzCustomerOrder zzCustomerOrder, @RequestParam("arr1") Integer[] arr1,@RequestParam("arr2") Integer[] arr2)
    {

        int x = 0;

        try {
            int sum = 0;
            //得出总价格
            for (int i = 0; i < arr1.length ; i++) {
                ZzProduct zzProduct = zzProductService.selectZzProductById(arr1[i]);

                sum+=zzProduct.getProductPrice()*arr2[i];
            }
            zzCustomerOrder.setCustomerOrderTotalPrice(sum);
            //向订单表中添加
            zzCustomerOrderService.insertZzCustomerOrder(zzCustomerOrder);
            //向订单明细表中添加
            for (int i = 0; i < arr1.length ; i++) {
                zzCustomerOrderItemService.insertZzCustomerOrderItem(new ZzCustomerOrderItem(null,zzCustomerOrder.getCustomerOrderId(),arr1[i],arr2[i]));
            }

            x=1;
        } catch (Exception e) {
            throw e;
        }

        return toAjax(x);
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{customerOrderId}")
    public String edit(@PathVariable("customerOrderId") Integer customerOrderId, ModelMap mmap)
    {
        ZzCustomerOrder zzCustomerOrder = zzCustomerOrderService.selectZzCustomerOrderById(customerOrderId);
        mmap.put("zzCustomerOrder", zzCustomerOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZzCustomerOrder zzCustomerOrder)
    {
        return toAjax(zzCustomerOrderService.updateZzCustomerOrder(zzCustomerOrder));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int i = 0;
        try {
            zzCustomerOrderItemService.deleteZzCustomerOrderItemByIds(ids);
            zzCustomerOrderService.deleteZzCustomerOrderByIds(ids);
            i=1;
        } catch (Exception e) {
            throw e;
        }
        return toAjax(i);
    }
}
